/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-02
 * Time: 21:50
 */
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread thread1 = new Thread(()->{
            //thread1拿到锁
            synchronized (locker) {
                System.out.println("thread1线程wait之前");
                try {
                    //thread1释放锁,进入waiting状态,等待被唤醒
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("thread1线程wait之后");
            }
        });
        Thread thread2 = new Thread(()->{
            //thread1进入waiting之后,thread2拿到锁
            synchronized (locker){
                System.out.println("thread2线程notify之前");
                //虽然notify执行之后thread1被唤醒了,但此时仍处于thread2的synchronized中
                //同一对象才能唤醒
                locker.notify();
                System.out.println("thread2线程notify之后");
            }
        });
        thread1.start();
        Thread.sleep(1000);
        thread2.start();
    }
}
